﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Text;

namespace Engine.DAL.Repositories
{
    public abstract class RepositoryBase<T> : IRepository<T> where T : class
    {
        protected abstract ObjectSet<T> ObjectSet { get; }

        public void Add(T value)
        {
            ObjectSet.AddObject(value);
        }

        public abstract void Update(T value);

        public void Delete(T value)
        {
            ObjectSet.DeleteObject(value);
        }

        public IEnumerable<T> GetValues()
        {
            return ObjectSet.ToList();
        }

        protected IQueryable<T> GetValues(params string[] includes)
        {
            return Include(includes);
        }

        private ObjectQuery<T> Include(string[] names, int index = 0)
        {
            if (index < 0)
            {
                throw new ArgumentException("index < 0 - не может быть меньше нуля!");
            }
            if (index >= names.Length)
            {
                return ObjectSet;
            }
            return Include(names, index + 1).Include(names[index]);
        }
    }
}
